/**
 * <b>项目名：</b>hclv-web<br/>  
 * <b>包名：</b>com.flea.hclv.service.impl<br/>  
 * <b>文件名：</b>HServiceOrgServiceImpl.java<br/>  
 * <b>版本信息：</b>1.0<br/>  
 * <b>日期：</b>2018年10月8日 下午7:18:21<br/>  
 * <b>COPYRIGHT 2010-2012 ALL RIGHTS RESERVED 虫子科技</b>-版权所有<br/>
 */
package com.flea.hclv.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HAddressMapper;
import com.flea.hclv.model.dao.HServicePartnerMapper;
import com.flea.hclv.model.entity.HAddress;
import com.flea.hclv.model.entity.HServicePartner;
import com.flea.hclv.service.HServiceOrgService;
import com.flea.system.base.util.StringUtil;
import com.flea.system.bean.FleaPage;
import com.flea.system.dao.OrgDeptMapper;
import com.flea.system.entity.OrgDept;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;

/**
 * @description
 * @createTime 2018年10月8日 下午7:18:21
 * @modifyTime
 * @author myflea@163.com
 * @version 1.0
 */
@Service("serviceOrgService")
public class HServiceOrgServiceImpl implements HServiceOrgService {
	@Autowired
	private OrgDeptMapper orgDeptMapper;
	@Autowired
	private HAddressMapper addressMapper;
	@Autowired
	private HServicePartnerMapper servicePartnerMapper;

	@Override
	public boolean saveOrUpdate(HttpServletRequest request, String serviceVo) {
		// json字符串中有这两个对象的属性值，就直接使用者两个属性的值
		OrgDept serviceBaseInfo = JSONObject.parseObject(serviceVo,OrgDept.class);
		HAddress addressInfo = JSONObject.parseObject(serviceVo, HAddress.class);
		String userId = FleaOrgUtils.getCurUserUuid(request);

		// 判断组织结构是增加还是修改

		String deptId = serviceBaseInfo.getDeptUuid();
		serviceBaseInfo.setModifyUser(userId);
		boolean saveServiceInfoResult = false;
		if (StringUtil.isEmpty(deptId)) {
			deptId = FleaStringUtils.getGenerateUUID();
			serviceBaseInfo.setCreateUser(userId);
			serviceBaseInfo.setDeptUuid(deptId);
			//确定为同一个体系结构中的人员
			serviceBaseInfo.setExtend2(serviceBaseInfo.getDeptCode());
			// 设置一个从code值
			serviceBaseInfo.setDeptTreeId(serviceBaseInfo.getDeptCode());
			serviceBaseInfo.setDeptLevel(HConstant.HCLV_ROOT_DEPT_LEVEL_CODE);
			saveServiceInfoResult = orgDeptMapper.insertSelective(serviceBaseInfo) > 0;
			
		} else {
			saveServiceInfoResult = orgDeptMapper.updateByPrimaryKeySelective(serviceBaseInfo) > 0;
		}

		// 设置地址，判断地址是增加还是删除
		addressInfo.setModifyUser(userId);
		String addressInfoId = addressInfo.getAddRessId();
		boolean addressResult = false;
		if (saveServiceInfoResult) {
			addressInfo.setModifyUser(userId);
			if (StringUtil.isEmpty(addressInfoId)) {
				addressInfoId = FleaStringUtils.getGenerateUUID();
				addressInfo.setId(addressInfoId);
				addressInfo.setCreateUser(userId);
				addressInfo.setUuid(deptId);
				addressResult = addressMapper.insertSelective(addressInfo) > 0;
			} else {
				addressInfo.setId(addressInfoId);
				addressResult = addressMapper.updateByPrimaryKeySelective(addressInfo) > 0;
			}
		}

		return addressResult;
	}

	@Override
	public FleaPage<OrgDept> findForGird(String queryName, String serviceType,
			String queryProvince, String queryCity, String queryZone,
			String querySatus, int curPage, int pageSize) {
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		// 根据查询获取内容
		List<HAddress> address = addressMapper.selectByParams(queryProvince, queryZone, queryZone);
		
		//获取所有的uuid，拼装成一个list， 避在底层orgdept类中，引入address相关的内容
		List<String> uuids = new ArrayList<String>();
		for (HAddress addressEntity : address) {
			uuids.add(String.valueOf(addressEntity.getUuid()));
		}
		List<OrgDept> pageData = orgDeptMapper.findByComParams(rowBounds,queryName,serviceType,querySatus,uuids);
		
		

		FleaPage<OrgDept> page = new FleaPage<OrgDept>(pageData);
		return page;

	}

	@Override
	public boolean deleteByIds(String ids, String userId) {
		List<String>  idsArray = JSON.parseArray(ids, String.class);
		addressMapper.deleteByUuids(idsArray);
		int result = orgDeptMapper.deleteByPrimaryKeys(idsArray);
		return result == idsArray.size() && result > 0;
	}

	@Override
	public boolean updateDeletedFrozon(String ids, String userId) {
		List<String>  idsArray = JSON.parseArray(ids, String.class);
		int result = orgDeptMapper.updateDeletedByPrimaryKeys(1,idsArray);
		return result == idsArray.size() && result > 0;
	}

	@Override
	public boolean updateDeletedcalFrozon(String ids, String userId) {
		List<String>  idsArray = JSON.parseArray(ids, String.class);
		int result = orgDeptMapper.updateDeletedByPrimaryKeys(0,idsArray);
		return result == idsArray.size() && result > 0;
	}

	@Override
	public List<OrgDept> queryOrgDeptTreeForAuth(String deptId) {
		   List<OrgDept> result = new ArrayList<OrgDept>();
		   result.add(orgDeptMapper.selectByPrimaryKey(deptId));
	        return result;
	}

	@Override
	public boolean saveOrUpdateDeptMent(HttpServletRequest request,String serviceVo) {

		// json字符串中有这两个对象的属性值，就直接使用者两个属性的值
		OrgDept serviceBaseInfo = JSONObject.parseObject(serviceVo,OrgDept.class);
		HAddress addressInfo = JSONObject.parseObject(serviceVo, HAddress.class);
		String userId = FleaOrgUtils.getCurUserUuid(request);

		// 判断组织结构是增加还是修改

		String deptId = serviceBaseInfo.getDeptUuid();
		serviceBaseInfo.setModifyUser(userId);
		boolean saveServiceInfoResult = false;
		if (StringUtil.isEmpty(deptId)) {
			deptId = FleaStringUtils.getGenerateUUID();
			serviceBaseInfo.setCreateUser(userId);
			serviceBaseInfo.setDeptUuid(deptId);
			
			OrgDept dept =	FleaOrgUtils.getCurrentUserDept(request);
			String deptExtend2 = "";
			if(null!=dept){
				deptExtend2 = dept.getExtend2();
			}
			serviceBaseInfo.setExtend2(deptExtend2);
			
			
			// 设置一个从code值
			serviceBaseInfo.setDeptTreeId(serviceBaseInfo.getDeptCode());
			serviceBaseInfo.setDeptLevel(HConstant.HCLV_ROOT_DEPT_SECONDE_LEVEL_CODE);
			OrgDept pDept = FleaOrgUtils.getCurrentUserDept(userId);
			if(null!=pDept){
				String pDeptTreeId = pDept.getDeptTreeId();
				serviceBaseInfo.setpDeptTreeId(pDeptTreeId);
			}else{
				serviceBaseInfo.setpDeptTreeId(HConstant.HCLV_ADMIN_ADMIN);
			}
			
			
			
			saveServiceInfoResult = orgDeptMapper.insertSelective(serviceBaseInfo) > 0;
			
		} else {
			saveServiceInfoResult = orgDeptMapper.updateByPrimaryKeySelective(serviceBaseInfo) > 0;
		}

		// 设置地址，判断地址是增加还是删除
		addressInfo.setModifyUser(userId);
		String addressInfoId = addressInfo.getAddRessId();
		boolean addressResult = false;
		if (saveServiceInfoResult) {
			addressInfo.setModifyUser(userId);
			if (StringUtil.isEmpty(addressInfoId)) {
				addressInfoId = FleaStringUtils.getGenerateUUID();
				addressInfo.setId(addressInfoId);
				addressInfo.setCreateUser(userId);
				addressInfo.setUuid(deptId);
				addressResult = addressMapper.insertSelective(addressInfo) > 0;
			} else {
				addressInfo.setId(addressInfoId);
				addressResult = addressMapper.updateByPrimaryKeySelective(addressInfo) > 0;
			}
		}

		return addressResult;
	
	}

	@Override
	public List<OrgDept> findeByExtend2(HttpServletRequest request) {
//		String deptExtend = 
		OrgDept dept =	FleaOrgUtils.getCurrentUserDept(request);
		String deptExtend2 = "";
		if(null!=dept){
			deptExtend2 = dept.getExtend2();
		}
		List<OrgDept>  result = orgDeptMapper.findByExtend2(deptExtend2);
		return result;
	}

	@Override
	public FleaPage<OrgDept> findPatnersForGird(String serviceOrgId,String queryName,
			String serviceType, String queryProvince, String queryCity,
			String queryZone, String querySatus, int curPage, int pageSize) {
		RowBounds rowBounds = new RowBounds(curPage, pageSize);
		// 根据查询获取内容
		List<HAddress> address = addressMapper.selectByParams(queryProvince, queryZone, queryZone);
		//获取所有的uuid，拼装成一个list， 避在底层orgdept类中，引入address相关的内容
		List<String> uuids = new ArrayList<String>();
		for (HAddress addressEntity : address) {
			uuids.add(String.valueOf(addressEntity.getUuid()));
		}

		List<HServicePartner> patners = servicePartnerMapper.findByServiceId(serviceOrgId);
		List<String> patnerIds = new ArrayList<String>();
		for (HServicePartner p : patners) {
			patnerIds.add(p.getPartnerId());
		}
		
		List<OrgDept> pageData = orgDeptMapper.findByComParamsAndPatnerIds(rowBounds,queryName,serviceType,querySatus,uuids,patnerIds);
		FleaPage<OrgDept> page = new FleaPage<OrgDept>(pageData);
		return page;
	}

}
